#include "Customer.hh"
#include <fstream>
#include <iomanip>
#include <iostream>



Customer::Customer(const std::string& customerName, const std::string& savingsId, const std::string& checkingId)
    : name(customerName) {
    savingsAccount = std::make_unique<SavingsAccount>(savingsId, name);
    checkingAccount = std::make_unique<CheckingAccount>(checkingId, name);
}

bool Customer::generateReport(const std::string& filename) const {
    std::ofstream file(filename);
    if (!file.is_open()) {
        std::cout << "Error: Could not create report file: " << filename << "\n";
        return false;
    }

    file << "Customer Transaction Report for: " << name << "\n\n";
    file << "Savings Account: " << savingsAccount->getAccountId() << "\n";
    file << "Balance: " << std::fixed << std::setprecision(2) << savingsAccount->getBalance() << "\n";
    file << "Transactions:\n";
    file << std::left << std::setw(10) << "ID"
         << std::setw(20) << "Date"
         << std::setw(15) << "Type"
         << std::setw(10) << "Amount"
         << "Note\n";
    file << std::string(65, '-') << "\n";

    for (const auto& t : savingsAccount->getTransactions()) {
        file << std::left << std::setw(10) << t.getTransactionId()
             << std::setw(20) << t.getDate()
             << std::setw(15) << t.getType()
             << std::fixed << std::setprecision(2) << std::setw(9) << t.getAmount()
             << t.getNote() << "\n";
    }

    file << "\nChecking Account: " << checkingAccount->getAccountId() << "\n";
    file << "Balance: " << std::fixed << std::setprecision(2) << checkingAccount->getBalance() << "\n";
    file << "Transactions:\n";
    file << std::left << std::setw(10) << "ID"
         << std::setw(20) << "Date"
         << std::setw(15) << "Type"
         << std::setw(10) << "Amount"
         << "Note\n";
    file << std::string(65, '-') << "\n";

    for (const auto& t : checkingAccount->getTransactions()) {
        file << std::left << std::setw(10) << t.getTransactionId()
             << std::setw(20) << t.getDate()
             << std::setw(15) << t.getType()
             << std::fixed << std::setprecision(2) << std::setw(9) << t.getAmount()
             << t.getNote() << "\n";
    }

    file.close();
    std::cout << "Report generated as " << filename << "\n";
    return true;
}
